# -*- codeing=utf-8 -*-
# @Time :2021/12/15 17:43
# @Author : 宦
# @File :utils.py
# @Software:PyCharm
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall import settings
from itsdangerous import BadSignature, BadData, BadHeader, BadTimeSignature, BadPayload \
    # 加密


def generic_openid(openid):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_id=3600)
    access_token = s.dump({'openid': openid})

    # 将bytes类型的数据转换为 str
    return access_token.decode()


# 解密
def check_access_token(token):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    try:
        result = s.loads(token)
    except Exception:
        return None

    else:
        return result.get('openid')
